<template>
  <jke-modal
    :visible.sync="jkeForm.visible"
    :title="jkeForm.title"
    :fullscreen.sync="jkeForm.fullscreen"
    :switchFullscreen="jkeForm.switchFullscreen"
  >
    <!--  -->
    <!-- <crumbs @goBack="goBack" :superiorMenu="['库存管理', '库存盘点']" :currentMenu="jkeForm.title"></crumbs> -->
    <div class="form-body">
      <a-card :bordered="false" style="margin-top: 7px">
        <a-form :form="form" @submit="handleSubmit" layout="vertical">
          <a-form-item label="单位名称：" required :labelCol="{ md: 6, xs: 24 }" :wrapperCol="{ md: 18, xs: 24 }">
            <a-input
              placeholder="请输入"
              :maxLength="30"
              v-decorator="[
                'unitName',
                {
                  rules: [
                    {
                      required: true,
                      message: '请输入单位名称',
                    },
                    {
                      message: '单位已存在！',
                      validator: (rule, value, callback) => validateunitName(value, callback),
                    },
                  ],
                },
              ]"
            ></a-input>
          </a-form-item>
          <a-form-item label="备注：" :labelCol="{ md: 6, xs: 24 }" :wrapperCol="{ md: 18, xs: 24 }">
            <a-textarea
              placeholder="请输入"
              :maxLength="500"
              v-decorator="[
                'unitNotes',
                {
                  rules: [],
                },
              ]"
            ></a-textarea>
          </a-form-item>
          <a-form-item> </a-form-item>
        </a-form>
      </a-card>
    </div>
    <div class="footer-submit form-footer">
      <a-button @click="closeBtn" class="submitBtn" style="margin-right: 10px"> 取消 </a-button>
      <a-button type="primary" :loading="loadingSubmit" class="submitBtn" @click="saveForm()"> 确定 </a-button>
    </div>
  </jke-modal>
</template>
<script>
import { FormTypes } from '@/utils/JEditableTableUtil'
import crumbs from '@/components/crumbs.vue'
import { getDictModel, productGetDictPageList } from '@/api/goodsManagement'
import { getFileListByIds } from '@/api/api'
import FileUpload from '@/components/fileActions/FileUpload.vue'
import { API } from '@/api/requestFzs'
import { apiBaseUrlDownload } from '@/utils/request'
import { APILX } from '@/api/requestLx'
import { validateDuplicateValue } from '@/utils/util'
import { fourFloatNum, accMul, accDivMin } from '@/utils/util'
import Examine from '@/components/examine'
export default {
  name: 'customerForm', //新建客户
  data() {
    return {
      jkeForm: {
        title: '新建单位',
        visible: false,
        fullscreen: false,
        // switchFullscreen: true,
      },
      processDataVo: '',
      sendType: '',
      isLoading: false, //防止tab来回切换 导致数据重复渲染
      tabNor: '待盘点（0）',
      tabAbNor: '已盘点（0）',
      addOrEdit: true, //待盘点 true 已盘点false
      inventoryUserIdText: '',
      inventoryUserIdRealId: '',
      signUser: '', //盘点人 id
      warehouseIdEdit: '', //继续盘点 仓库ID
      domianURL: apiBaseUrlDownload, //表格图片展示
      getGoodsId: '', //选仓库取到的id
      //新建时候 获取明细
      inventoryTypeList: [], //盘点类型下拉
      warehouseIdList: [], //仓库数据下拉
      selectRowKeys: [],
      selectRowKeysEd: [],
      selectGoodsIds: [],
      selectGoodsIdsEd: [],
      validateDuplicateValue, //自动生成编码
      queryId: '',
      loadingSubmit: false, //防止多次提交
      submitApi: APILX.basicUnitAdd,
      isEdit: false,
      mainDivWidth: 0,
      attachment: '', //附件
      form: this.$form.createForm(this, { name: 'coordinated' }),
      reloadForm: false,
    }
  },
  components: {
    FileUpload,
    Examine,
    crumbs,
  },
  created() {
    this.getdata()
  },
  mounted() {
    let mainDiv = document.querySelector('.ant-layout-content') //ant-layout-content
    this.mainDivWidth = `${mainDiv.clientWidth}px`
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
  },
  methods: {
    // 选择合格品单位
    qualifiedChange(e, props, key) {
      let { unitList, rateQOld, inventoryGoodsQualifiedNum } = this.$refs[key].getValuesSync({
        validate: false,
        rowIds: [props.rowId],
      }).values[0]
      // console.log('inventoryGoodsQualifiedNum:',inventoryGoodsQualifiedNum,'-','rateQOld:',rateQOld);
      let old_num = inventoryGoodsQualifiedNum
        ? accMul(inventoryGoodsQualifiedNum, rateQOld)
        : '' /* 当前数值乘以单位换算比例，得到对应的基本单位数量 */
      let unit = JSON.parse(unitList).find((item) => item.id == e)
      this.$refs[key].setValues([
        {
          rowKey: props.rowId,
          values: {
            rateQ: unit.conversionRate,
            rateQOld: unit.conversionRate,
            selectUnitQ: e,
            inventoryGoodsQualifiedNum: inventoryGoodsQualifiedNum
              ? accDivMin(old_num, unit.conversionRate)
              : '' /* 基本单位数量除以辅助单位比例，得到当前数值对应辅助单位数量 */,
          },
        },
      ])
    },
    // 选择报废品单位
    scrapNumChange(e, props, key) {
      let { unitList, rateSOld, inventoryGoodsScrapNum } = this.$refs[key].getValuesSync({
        validate: false,
        rowIds: [props.rowId],
      }).values[0]
      let old_num = inventoryGoodsScrapNum ? accMul(inventoryGoodsScrapNum, rateSOld) : ''
      let unit = JSON.parse(unitList).find((item) => item.id == e)
      this.$refs[key].setValues([
        {
          rowKey: props.rowId,
          values: {
            rateS: unit.conversionRate,
            rateSOld: unit.conversionRate,
            selectUnitS: e,
            inventoryGoodsScrapNum: inventoryGoodsScrapNum ? accDivMin(old_num, unit.conversionRate) : '',
          },
        },
      ])
    },
    dataSourceItem(props, key, ediKey) {
      let item = this.$refs[ediKey].getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      let val = item[key]
      if (key == 'unitList') {
        return JSON.parse(val)
      } else {
        return val
      }
    },
    closeBtn() {
      this.jkeForm.visible = false
      // this.submitProcess()
    },
    // 提交（必须使用名为submitProcess方法进行提交）
    submitProcess(e) {
      if (e && e.nodeDtos) {
        this.processDataVo = e
        // console.log(e, this.processDataVo, 'processDataVo')
        // 获取到数据后写表单提交的方法
        this.saveForm(this.sendType)
      } else {
        // 调用子组件的方法获取数据
        this.$refs.examine.confirm()
      }
    },
    // tab分页
    changeCurrent(key) {
      if (key == '1') {
        // this.selectRowKeys = []
        this.addOrEdit = false
        // this.getAbleTableList() //已盘点
      } else {
        // this.selectRowKeys = []
        this.addOrEdit = true // 待盘点
        // this.getEditUnAbleTable()
      }
      //   this.getData()
    },
    // 分页
    onChangePage(current, pageSize) {
      this.getGoodsListPage.current = current
      if (this.queryId) {
        this.getEditUnAbleTable()
      } else {
        this.getGoodsList()
      }
    },
    onShowSizeChange(current, pageSize) {
      this.getGoodsListPage.pageSize = pageSize
      this.getGoodsListPage.current = current
      if (this.queryId) {
        this.getEditUnAbleTable()
      } else {
        this.getGoodsList()
      }
    },

    selectRowChange(ids) {
      this.selectRowKeys = ids
      if (this.selectRowKeys.length !== 0) {
        this.selectRowKeys.forEach((item) => {
          var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [item] }).values[0]
          if (!tableValue.inventoryGoodsQualifiedNum && !tableValue.inventoryGoodsScrapNum) {
            this.$refs.goodsTable.setValues([
              {
                rowKey: item, // 行的id
                values: {
                  // 在这里 values 中的 specifications 是你 columns 中配置的 key
                  inventoryGoodsQualifiedNum: tableValue.goodsQualifiedNum
                    ? accDivMin(tableValue.goodsQualifiedNum, tableValue.rateQ)
                    : '',
                  inventoryGoodsScrapNum: tableValue.goodsScrapNum
                    ? accDivMin(tableValue.goodsScrapNum, tableValue.rateS)
                    : '',
                  pdAmountExcludingTax: tableValue.amountExcludingTax,
                },
              },
            ])
          }
        })
      }
    },
    validateunitName(value, callback) {
      APILX.basicUnitQueryUnit({ unitName: value, id: this.queryId }).then((res) => {
        console.log(res)
        !res.result ? callback() : callback('单位值不能重复')
      })
    },
    // 待盘点
    getEditUnAbleTable() {
      let param = {
        id: this.queryId,
        warehouseId: this.warehouseIdEdit,
        pageNo: this.getGoodsListPage.current,
        pageSize: this.getGoodsListPage.pageSize,
      }
      this.goodsTable.dataSource = []
      this.isLoading = true
      // console.log(this.isLoading, ' getEditUnAbleTable isLoadingisLoadingisLoading')
      APILX.warehouseInventoryUnCompleteList(param).then(async (res) => {
        if (res.code == 200) {
          // console.log(res, '进来了没warehouseInventoryUnCompleteList')
          let tableData = res.result.records
          for (let i = 0; i < tableData.length; i++) {
            let item = tableData[i]
            const unitRes = await API.findUnitListByGoodsIdSync({ goodsId: item.goodsId })
            item.unitList = JSON.stringify(unitRes.result)
            item.rateQ = 1
            item.rateS = 1
            item.rateQOld = 1
            item.rateSOld = 1
            item.selectUnitQ = unitRes.result[unitRes.result.length - 1].id
            item.selectUnitS = unitRes.result[unitRes.result.length - 1].id
          }
          tableData.forEach((item) => {
            if (item.attachment) {
              let ids = item.attachment
              getFileListByIds({ ids }).then((res) => {
                if (res.code == 200) {
                  let fileList = ''
                  fileList = res.result
                  if (fileList.length > 0) {
                    item.picUrl = this.domianURL + fileList[0].uri
                    this.goodsTable.dataSource.push(item)
                  } else {
                    item.picUrl = require('../../../assets/img/noPicture.jpg')
                    this.goodsTable.dataSource.push(item)
                  }
                }
              })
            } else {
              item.picUrl = require('../../../assets/img/noPicture.jpg')
              this.goodsTable.dataSource.push(item)
            }
          })
          // this.goodsTable.dataSource = tableData
          console.log(this.goodsTable.dataSource, 'this.goodsTable.dataSource')
          this.getGoodsListPage.total = res.result.total
          setTimeout(() => {
            this.isLoading = false
          }, 1000)
        } else {
          this.$antdMessage.error(res.message)
          this.isLoading = false
        }
      })
    },
    //已盘点 table
    // 分页
    onChangePageEd(current, pageSize) {
      this.getGoodsListPageEd.current = current
      this.getAbleTableList()
    },
    onShowSizeChangeEd(current, pageSize) {
      this.getGoodsListPageEd.pageSize = pageSize
      this.getGoodsListPageEd.current = current
      this.getAbleTableList()
    },

    selectRowChangeEd(ids) {
      this.selectRowKeysEd = ids
      console.log(this.selectRowKeysEd, 'selectRowChangeEd')
      if (this.selectRowKeysEd.length !== 0) {
        this.selectRowKeysEd.forEach((item) => {
          var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [item] }).values[0]
          this.selectGoodsIdsEd.push(tableValue.goodsId)
          if (!tableValue.inventoryGoodsQualifiedNum && !tableValue.inventoryGoodsScrapNum) {
            this.$refs.goodsTableEd.setValues([
              {
                rowKey: item, // 行的id
                values: {
                  // 在这里 values 中的 specifications 是你 columns 中配置的 key
                  // inventoryGoodsQualifiedNum: tableValue.goodsQualifiedNum || '',
                  // inventoryGoodsScrapNum: tableValue.goodsScrapNum || '',
                  inventoryGoodsQualifiedNum: tableValue.goodsQualifiedNum
                    ? accDivMin(tableValue.goodsQualifiedNum, tableValue.rateQ)
                    : '',
                  inventoryGoodsScrapNum: tableValue.goodsScrapNum
                    ? accDivMin(tableValue.goodsScrapNum, tableValue.rateS)
                    : '',
                },
              },
            ])
          }
        })
      }
    },
    // 已盘点 编辑
    getAbleTableList() {
      console.log('selectRowKeysEd', this.selectGoodsIdsEd)
      let param = {
        id: this.queryId,
        pageNo: this.getGoodsListPageEd.current,
        pageSize: this.getGoodsListPageEd.pageSize,
      }
      this.goodsTableEd.dataSource = []
      this.isLoading = true
      // console.log(this.isLoading, ' getAbleTableList isLoadingisLoadingisLoading')
      APILX.wmsWarehouseInventoryCompleteList(param).then(async (res) => {
        if (res.code == 200) {
          // console.log(res, '进来了没warehouseInventoryUnCompleteList')
          let tableData = res.result.records
          for (let i = 0; i < tableData.length; i++) {
            let item = tableData[i]
            const unitRes = await API.findUnitListByGoodsIdSync({ goodsId: item.goodsId })
            item.unitList = JSON.stringify(unitRes.result)
            if (item.goodsQualifiedUnitId) {
              let unitQ = unitRes.result.find((u) => u.id == item.goodsQualifiedUnitId)
              let unitS = unitRes.result.find((u) => u.id == item.goodsScrapUnitId)
              item.selectUnitQ = unitQ ? unitQ.id : unitRes.result[unitRes.result.length - 1].id
              item.selectUnitS = unitS ? unitS.id : unitRes.result[unitRes.result.length - 1].id

              item.rateQ = unitQ ? unitQ.conversionRate : 1
              item.rateQOld = unitQ ? unitQ.conversionRate : 1

              item.rateS = unitS ? unitS.conversionRate : 1
              item.rateSOld = unitS ? unitS.conversionRate : 1

              item.inventoryGoodsQualifiedNum = accDivMin(item.inventoryGoodsQualifiedNum, item.rateQ)
              item.inventoryGoodsScrapNum = accDivMin(item.inventoryGoodsScrapNum, item.rateS)
            } else {
              item.rateQ = 1
              item.rateS = 1
              item.rateQOld = 1
              item.rateSOld = 1
              item.selectUnitQ = unitRes.result[unitRes.result.length - 1].id
              item.selectUnitS = unitRes.result[unitRes.result.length - 1].id
            }
          }
          tableData.forEach((item) => {
            if (item.attachment) {
              let ids = item.attachment
              getFileListByIds({ ids }).then((res) => {
                if (res.code == 200) {
                  let fileList = ''
                  fileList = res.result
                  if (fileList.length > 0) {
                    item.picUrl = this.domianURL + fileList[0].uri
                    this.goodsTableEd.dataSource.push(item)
                  } else {
                    item.picUrl = require('../../../assets/img/noPicture.jpg')
                    this.goodsTableEd.dataSource.push(item)
                  }
                }
              })
            } else {
              item.picUrl = require('../../../assets/img/noPicture.jpg')
              this.goodsTableEd.dataSource.push(item)
            }
            if (item.inventoryGoodsQualifiedNum == '-') {
              item.inventoryGoodsQualifiedNum = 0.0
            }
            if (item.inventoryGoodsScrapNum == '-') {
              item.inventoryGoodsScrapNum = 0.0
            }
          })
          this.getGoodsListPageEd.total = res.result.total
          let _that = this
          setTimeout(() => {
            _that.isLoading = false
            console.log(_that.selectGoodsIdsEd, 'this.goodsTableEd.dataSource')
            const { error, values } = _that.$refs.goodsTableEd.getValuesSync({ validate: false })
            let ids = []
            let goods = values.filter((item) => _that.selectGoodsIdsEd.includes(item.goodsId))
            // console.log('aaaaaa',aaa);
            for (let g = 0; g < goods.length; g++) {
              ids.push(goods[g].id)
            }
            _that.$refs.goodsTableEd.setSelection(ids)
          }, 1000)
          // this.isLoading = false
        } else {
          this.$antdMessage.error(res.message)
          this.isLoading = false
        }
      })
    },

    /* 获取编辑详情 */
    initForm(v) {
      this.jkeForm.visible = true
      // this.$store.commit('SET_CURRENT_PREFIX', '/jksaas-easyMake/easyMake')
      if (v) {
        this.queryId = v.id
        this.jkeForm.title = '编辑单位'
        // console.log(id, '00.0.0.0')
        this.submitApi = APILX.basicUnitEdit
        this.isEdit = true
        let _this = this
        setTimeout(() => {
          _this.form.setFieldsValue(v)
        }, 100)
      } else {
        this.queryId = null
        this.jkeForm.title = '新建单位'
        this.isEdit = false
        this.submitApi = APILX.basicUnitAdd
      }
    },

    // 提交
    saveForm(all) {
      this.handleSubmit([], all)
    },
    // 提交
    saveFormEd(newparam1, all) {
      // console.log(all, 'all')
      if (this.selectRowKeysEd.length !== 0) {
        const { error, values } = this.$refs.goodsTableEd.getValuesSync({ validate: true })
        let newparamEd = values.filter((item) => this.selectRowKeysEd.includes(item.id))
        //合并两个数组
        let newparam = newparam1.concat(newparamEd)
        // console.log(newparam, 'newparam')
        this.handleSubmit(newparam, all)
      } else {
        this.handleSubmit(newparam1, all)
      }
      // console.log(all, 'all2')
    },
    handleSubmit(tableValue, all) {
      this.form.validateFields((err, values) => {
        console.log(values, 'values form')
        if (!err) {
          this.submitTo(tableValue, values, all)
        }
      })
    },
    submitTo(tableValue, values, all) {
      // console.log(all, 'all 进入最终submit')
      // console.log(tableValue, 'tableValue')

      let parameters = {
        ...values,
        id: this.queryId,
      }

      this.loadingSubmit = true
      this.submitApi(parameters).then((res) => {
        if (res.code == 200) {
          this.loadingSubmit = false
          let subMsg = this.isEdit ? '编辑单位成功！' : '新建单位成功！'
          this.$antdMessage.success(subMsg)
          this.selectRowKeys = []
          this.selectRowKeysEd = []
          if (this.reloadForm) {
            if (!this.queryId) {
              this.queryId = res.result
            }
            if (tableValue.length === dataList.values.length) {
              this.getGoodsListPage.current = this.getGoodsListPage.current == 1 ? 1 : this.getGoodsListPage.current - 1
            }
            this.initForm(this.queryId)
            // this.getGoodsList()
            // this.getEditUnAbleTable()
          } else {
            this.goBack()
          }
        } else {
          this.loadingSubmit = false
          this.$antdMessage.error(res.message)
        }
      })
    },
    submitAll(tableValue, values, all) {
      // tableValue.forEach((item) => {
      //   if (item.inventoryGoodsQualifiedNum == '') {
      //     item.inventoryGoodsQualifiedNum = 0
      //   }
      //   if (item.inventoryGoodsScrapNum == '') {
      //     item.inventoryGoodsScrapNum = 0
      //   }
      //   item.inventoryGoodsQualifiedNum = accMul(item.inventoryGoodsQualifiedNum, item.rateQ)
      //   item.inventoryGoodsScrapNum = accMul(item.inventoryGoodsScrapNum, item.rateS)
      // })
      let that = this
      that.$antdConfirm({
        title: '确定盘点完毕吗?',
        content: `盘点完毕后，无法更改盘点数据`,
        onOk() {
          // console.log(all, 'all 弹窗')
          that.submitTo(tableValue, values, all)
        },
        onCancel() {},
      })
    },
    //下拉选择数据接口
    getdata() {
      // APILX.wmsWarehouseQueryList().then((res) => {
      //   if (res.code == 200) {
      //     this.warehouseIdList = res.result
      //   } else {
      //     that.$antdMessage.error(res.message)
      //   }
      // })
      //   取盘点类型
      // let parametersType = { dictCode: 'inventory_type' }
      // getDictModel(parametersType).then((res) => {
      //   // console.log(res, 'resType')
      //   if (res.code == 200) {
      //     this.inventoryTypeList = res.result
      //   } else {
      //     that.$antdMessage.error(res.message)
      //   }
      // })
    },
    setColor(props) {
      // if (props.text == 0) {
      //   return '#0f0!important'
      // }
      var newNum = props.text
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      // console.log(tableValue, 'tableValuetableValuetableValue')
      if (Number(newNum) > tableValue.goodsQualifiedNum) {
        return '#ff4d4f'
      } else {
        return '#00b042'
      }
    },
    setColor2(props) {
      var newNum = props.text
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (Number(newNum) > tableValue.goodsScrapNum) {
        return '#ff4d4f'
      } else {
        return '#00b042'
      }
    },

    inputNum(e, props, inputKey) {
      let newNum = fourFloatNum(e.target.value)
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (inputKey == 'inventoryGoodsQualifiedNum') {
        if (Number(newNum) > tableValue.goodsQualifiedNum) {
          // this.$antdMessage.warning('库存不足：本次盘点合格品不得大于账面合格品')
        } else {
        }
      }
      if (inputKey == 'inventoryGoodsScrapNum') {
        if (Number(newNum) > tableValue.goodsScrapNum) {
          // this.$antdMessage.warning('库存不足：本次盘点报废品不得大于账面报废品')
        } else {
        }
      }
      this.$refs.goodsTable.setValues([
        {
          rowKey: props.rowId, // 行的id
          values: {
            [inputKey]: newNum,
          },
        },
      ])
      // var tableValue2 = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      // console.log(tableValue2, 'tableValue2tableValue2')
    },
    pdMoney(val, props, tableType) {
      this.$refs[tableType].setValues([
        {
          rowKey: props.rowId, // 行的id
          values: {
            pdAmountExcludingTax: val,
          },
        },
      ])
    },
    inputFocus(props) {
      // console.log(props, 'props')
      const isSelect = this.selectRowKeys.filter((each) => each == props.rowId)
      if (isSelect.length == 0) {
        // this.selectRowKeys.push(props.rowId)
        this.$antdMessage.error('请先勾选当前数据')
        // console.log(this.selectRowKeys, 'this.selectRowKeys')
      }
    },

    // 已盘点
    inputFocusEd(props) {
      // console.log(props, 'props')
      const isSelect = this.selectRowKeysEd.filter((each) => each == props.rowId)
      if (isSelect.length == 0) {
        // this.selectRowKeysEd.push(props.rowId)
        this.$antdMessage.error('请先勾选当前数据')
        // console.log(this.selectRowKeysEd, 'this.selectRowKeysEd')
      }
    },
    setColorEd(props) {
      // console.log(props.text)
      // if (props.text == 0) {
      //   return '#0f0!important'
      // }
      var newNum = props.text
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      // console.log(tableValue, 'tableValuetableValuetableValue')
      if (Number(newNum) > tableValue.goodsQualifiedNum) {
        return '#ff4d4f'
      } else {
        return '#00b042'
      }
    },
    setColor2Ed(props) {
      var newNum = props.text
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (Number(newNum) > tableValue.goodsScrapNum) {
        return '#ff4d4f'
      } else {
        return '#00b042'
      }
    },

    inputNumEd(e, props, inputKey) {
      let newNum = fourFloatNum(e.target.value)
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (inputKey == 'inventoryGoodsQualifiedNum') {
        if (Number(newNum) > tableValue.goodsQualifiedNum) {
          // this.$antdMessage.warning('库存不足：本次盘点合格品不得大于账面合格品')
        } else {
        }
      }
      if (inputKey == 'inventoryGoodsScrapNum') {
        if (Number(newNum) > tableValue.goodsScrapNum) {
          // this.$antdMessage.warning('库存不足：本次盘点报废品不得大于账面报废品')
        } else {
        }
      }
      this.$refs.goodsTableEd.setValues([
        {
          rowKey: props.rowId, // 行的id
          values: {
            [inputKey]: newNum,
          },
        },
      ])
      // var tableValue2 = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      // console.log(tableValue2, 'tableValue2tableValue2')
    },

    // 已盘点结束

    /* 自动生成 */
    genEntityCode() {
      API.genEntityCode({ entityCode: 'WMS_WAREHOUSE_INVENTORY' }).then((res) => {
        if (res.code == 200) {
          this.form.setFieldsValue({
            inventoryCode: res.result,
          })
        } else {
          this.$antdMessage.error(res.message)
        }
      })
    },
    valueChange(type, row) {
      // console.log(type, row, 'value change type')
    },
    //   选仓库带明细
    selectOrder(v) {
      if (v) {
        this.getGoodsId = v
        this.getGoodsList()
      }
    },
    getGoodsList() {
      let param = {
        id: this.getGoodsId,
        pageNo: this.getGoodsListPage.current,
        pageSize: this.getGoodsListPage.pageSize,
      }
      this.goodsTable.dataSource = []
      this.isLoading = true
      APILX.getGoodsListByWarehouseId(param).then(async (res) => {
        if (res.result) {
          let tableData = res.result.records
          for (let i = 0; i < tableData.length; i++) {
            let item = tableData[i]
            const unitRes = await API.findUnitListByGoodsIdSync({ goodsId: item.goodsId })
            item.unitList = JSON.stringify(unitRes.result)
            item.rateQ = 1
            item.rateS = 1
            item.rateQOld = 1
            item.rateSOld = 1
            item.selectUnitQ = unitRes.result[unitRes.result.length - 1].id
            item.selectUnitS = unitRes.result[unitRes.result.length - 1].id
          }
          tableData.forEach((item) => {
            // const unitRes = await API.findUnitListByGoodsIdSync({goodsId: item.goodsId})
            // item.unitList = JSON.stringify(unitRes.result)
            // item.rateQ = 1
            // item.rateS = 1
            // item.selectUnitQ = unitRes.result[unitRes.result.length-1].id
            // item.selectUnitS = unitRes.result[unitRes.result.length-1].id
            if (item.attachment) {
              let ids = item.attachment
              getFileListByIds({ ids }).then((res) => {
                if (res.code == 200) {
                  let fileList = ''
                  fileList = res.result
                  if (fileList.length > 0) {
                    item.picUrl = this.domianURL + fileList[0].uri
                    this.goodsTable.dataSource.push(item)
                  } else {
                    item.picUrl = require('../../../assets/img/noPicture.jpg')
                    this.goodsTable.dataSource.push(item)
                  }
                }
              })
            } else {
              item.picUrl = require('../../../assets/img/noPicture.jpg')
              this.goodsTable.dataSource.push(item)
            }
          })
          // console.log(this.goodsTable.dataSource, 'this.goodsTable.dataSource')
          this.getGoodsListPage.total = res.result.total
          this.tabNor = `待盘点（${this.getGoodsListPage.total}）`
          this.isLoading = false
        }
      })
    },
    /* 面包屑返回 */
    goBack() {
      this.$emit('refresh')
      this.jkeForm.visible = false
    },
    /* 获取附件 */
    getFileList(v) {
      this.attachment = v
      // console.log('上传的附件id：', v)
    },
    /* 选盘点人 */
    userFilter(v, row) {
      // console.log(v, row)
      this.form.setFieldsValue({
        inventoryUserId: v,
      })
      if (row.length > 0) {
        this.signUser = row[0].id
      } else {
        this.signUser = ''
      }
    },
    // 下拉搜索
    filterOption(input, option) {
      // console.log('inputoption', input, option)
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    cancel() {},
    setGoodsScrapNum(props) {
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.goodsScrapNum) {
        return tableValue.goodsScrapNum
      } else {
        return ''
      }
    },
    setUnit(props) {
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.assistUnit) {
        return tableValue.assistUnit
      } else {
        return ''
      }
    },
    setSpec(props) {
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.spec) {
        return tableValue.spec
      } else {
        return '-'
      }
    },
    setName(props) {
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.goodsName) {
        return tableValue.goodsName
      } else {
        return ''
      }
    },
    setCode(props) {
      var tableValue = this.$refs.goodsTable.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.goodsCode) {
        return tableValue.goodsCode
      } else {
        return ''
      }
    },

    // 已盘点
    setGoodsScrapNumEd(props) {
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.goodsScrapNum) {
        return tableValue.goodsScrapNum
      } else {
        return ''
      }
    },
    // 辅助单位账面库存数量
    setNumAssistUnit(props, key, numTextKey) {
      var tableValue = this.$refs[key].getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      // console.log('setNumAssistUnit', tableValue)
      // return tableValue.goodsScrapNumTextAssistUnit || '-'
      return tableValue[numTextKey] || '-'
    },
    setUnitEd(props) {
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.assistUnit) {
        return tableValue.assistUnit
      } else {
        return ''
      }
    },
    setSpecEd(props) {
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.spec) {
        return tableValue.spec
      } else {
        return ''
      }
    },
    setNameEd(props) {
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.goodsName) {
        return tableValue.goodsName
      } else {
        return ''
      }
    },
    setCodeEd(props) {
      var tableValue = this.$refs.goodsTableEd.getValuesSync({ validate: false, rowIds: [props.rowId] }).values[0]
      if (tableValue.goodsCode) {
        return tableValue.goodsCode
      } else {
        return ''
      }
    },
  },
}
</script>
<style scoped lang="less">
@import '~@/assets/less/public.less';
.flex(@jc,@ai,@fd:row) {
  display: flex;
  justify-content: @jc;
  align-items: @ai;
  flex-direction: @fd;
}
/deep/.ant-form-vertical {
}
.form-body {
  // height: calc(55vh - 180px);
  // overflow-y: scroll;
}
.submitBtn {
  .flex(flex-end, center);
  padding: 8px;
  justify-content: center;
}
.code-btn {
  position: absolute;
  right: 0;
  top: -34.5px;
}
.pic-f {
  box-sizing: border-box;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 20px 10px;
  .pic-r {
    flex: 1;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #444444;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    .subDiv {
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
/deep/.ant-form-item-label {
  display: block;
  margin: 0;
  padding: 0px 0px 8px;
  line-height: 1.5;
  white-space: initial;
  text-align: right;
}
.imgClass {
  width: 60px;
  height: 60px;
  border-radius: 6px;
}

.my-input {
  padding: 4px 30px 4px 11px;
  border-radius: 3px;
  border: solid 1px #cdcdcd;
}
input::-webkit-input-placeholder {
  color: #cdcdcd;
}
input::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: #cdcdcd;
}
input:-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: #cdcdcd;
}
input:-ms-input-placeholder {
  /* Internet Explorer 10-11 */
  color: #cdcdcd;
}
</style>
